package org.spring.boot.fusion.controller;

import java.util.List;

import org.I0Itec.zkclient.ZkClient;
import org.spring.boot.fusion.tools.JsonTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("test")
	public String test() {
		List<String> services = discoveryClient.getServices();
		if (services != null && services.size() > 0) {
			for (String string : services) {
				List<ServiceInstance> instances = discoveryClient.getInstances(string);
				if (instances != null && instances.size() > 0) {
					for (ServiceInstance serviceInstance : instances) {
						System.out.println(string+":"+JsonTool.ObjectToJson(serviceInstance));
					}
				}
			}
		}
		return JsonTool.ObjectToJson(services);
	}
	
	public static void main(String[] args) {
		ZkClient zkClient = new ZkClient("192.168.20.25:2181");
		List<String> children = zkClient.getChildren("/services/fusion-zk-server");
		System.out.println(children);
		
//		boolean delete = zkClient.delete("/services/fusion-zk-server/7bbe8131-54b2-4110-8c24-a5c10bd2ca53");
//		System.out.println(delete);
	}
}
